package com.huiyun.project.im.server.biz.controller.sv;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huiyun.project.common.core.domain.R;
import com.huiyun.project.common.core.util.fn.FunctionUtils;
import com.huiyun.project.common.sequence.IdUtils;
import com.huiyun.project.im.core.service.IImMessageService;
import com.huiyun.project.im.entity.ImMessage;
import org.jim.core.ImConst;
import org.jim.core.packets.pub.pojo.JimChatBody;
import org.jim.core.packets.cs.session.vo.JimCsMessageReq;
import org.jim.core.packets.cs.session.vo.JimCsMessageResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.wf.jwtp.annotation.Inner;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("sv/message")
public class ImMessageController {
    @Autowired
    private IImMessageService messageService;

    @PostMapping("/save")
    @Inner
    public R save(@RequestBody JimChatBody chatBody) {
        return FunctionUtils.wrapper(() -> {
            ImMessage message = new ImMessage();
            BeanUtil.copyProperties(chatBody, message, "id");
            message.setId(Convert.toStr(IdUtils.getId()));
            message.setClientMsgId(chatBody.getId());
            return R.ok(messageService.save(message));
        });
    }

    @GetMapping("getList")
    @Inner(value = false)
    public R<List<JimCsMessageResp>> getList(JimCsMessageReq messageReq) {
        Page<ImMessage> page = null;
        if (ImConst.CS_OPERATE.equals(messageReq.getOperateType())) {
            page = messageService.page(new Page<>(messageReq.getPageNum(), messageReq.getPageSize()), Wrappers.<ImMessage>lambdaQuery()
                    .eq(ImMessage::getFromUserId, messageReq.getUserId())
                    .eq(ImMessage::getToUserId, messageReq.getCsUserId())
                    .or()
                    .eq(ImMessage::getFromUserId, messageReq.getCsUserId())
                    .eq(ImMessage::getToUserId, messageReq.getUserId())
                    .orderByDesc(ImMessage::getCreateTime)
                    .ge(null != messageReq.getBeginTime(), ImMessage::getCreateTime, messageReq.getBeginTime())
                    .le(null != messageReq.getEndTime(), ImMessage::getCreateTime, messageReq.getEndTime())

            );
        } else if (ImConst.USER_OPERATE.equals(messageReq.getOperateType())) {
            page = messageService.page(new Page<>(messageReq.getPageNum(), messageReq.getPageSize()), Wrappers.<ImMessage>lambdaQuery()
                    .eq(ImMessage::getFromUserId, messageReq.getUserId())
                    .or()
                    .eq(ImMessage::getToUserId, messageReq.getUserId())
                    .eq(ImMessage::getCsTenantId, messageReq.getCsTenantId())
                    .orderByDesc(ImMessage::getCreateTime)
                    .ge(null != messageReq.getBeginTime(), ImMessage::getCreateTime, messageReq.getBeginTime())
                    .le(null != messageReq.getEndTime(), ImMessage::getCreateTime, messageReq.getEndTime())
            );
        }
        List<JimCsMessageResp> list = new ArrayList<>();
        if (page!=null&&CollectionUtil.isNotEmpty(page.getRecords())) {
            JimCsMessageResp resp;
            for (ImMessage imMessage : page.getRecords()) {
                resp = new JimCsMessageResp();
                BeanUtil.copyProperties(imMessage, resp);
                list.add(resp);
            }
        }
        return R.ok(list);
    }

}
